// safe.txt
//
// A locked container. Like lockbox.txt, but warns
// the party if there is a key needed. Has also the abi-
// lity to be magically locked, when the party has
// to cast dispel barrier before opening the lock.
//
// Cell 0: Lock level. The tool use skill needed to pick the padlock.
//         Or, the strenght required to pull off the padlock.
//
// Cell 1: If 0, ignored. Otherwise, if the party has this special
//         item, the container is automatically accessable.
//
// Cell 2, 3: A SDF set to 1 when the lock is opened.
//            Later, the box is automatically accessable
//            if this flag is set to 1.
//
// Cell 4: If 0, ignored. Otherwise, the lock is magical and
//         dispel barrier must be cast before
//         the inside is accessable.

beginterrainscript;
variables;
   int open = 0, choice, result;
body;
beginstate INIT_STATE;
       set_physical_strength(get_memory_cell(0));
       set_mechanism_difficulty(get_memory_cell(0));
       if (get_flag(get_memory_cell(2), get_memory_cell(3)) != 0)
           open = 1;
break;
beginstate START_STATE;
break;
beginstate SEARCH_STATE;
       if ((get_memory_cell(1) != 0) && (has_special_item(get_memory_cell(1) && open == 0))) {
           print_str_color("You have the key which unlocks the container.", 2);
           open = 1;
           }
       if ((get_memory_cell(4) != 0) && (open == 0)) {
           message_dialog("This container is locked. You try to examine the padlock, but it is surrounded by a faint field of energy. You can't reach it until you remove the enchantment with a spell of your own.", "(You need to cast Dispel Barrier before you can pick the lock.)");
           block_entry(1);
           }
       if ((get_memory_cell(4) == 0) && (open == 0)) {
           reset_dialog();
           if (get_memory_cell(1) != 0)
               add_dialog_str(0, "This container is locked, and you don't have the key. You can have your strongest character try to break the padlock (which requires high strength) or have your most skilled character try to pick the lock (which requires Tool Use skill and lockpicks).",0);
           else add_dialog_str(0, "This container is locked. You can have your strongest character try to break the padlock (which requires high strength) or have your most skilled character try to pick the lock (which requires Tool Use skill and a lockpick).",0);
           add_dialog_choice(0, "Leave the container alone.");
           add_dialog_choice(1, "Try to break the padlock.");
           add_dialog_choice(2, "Try to pick the padlock.");
           choice = run_dialog(0);
           if (choice == 1)
                block_entry(1);
           if (choice == 2)
                set_state_continue(10);
           if (choice == 3)
                set_state_continue(11);
           }
break;
beginstate 10;
       if (get_highest_skill(0) >= get_physical_strength()) {
           play_sound(129);
           print_big_str("You have managed to pull off the padlock. (Difficulty:", get_memory_cell(0), ")");
           open = 1;
           }
       else {
           print_big_str("Failed: You're not strong enough. (Difficulty:", get_memory_cell(0), ")");
           play_sound(41);
           block_entry(1);
           }
break;
beginstate 11;
       result = run_pick_lock(get_mechanism_difficulty());
       if (result == 1) {
           play_sound(160);
           award_party_xp(BASE_TRAP_XP, 1 + 2 * get_mechanism_difficulty());
           if ((get_memory_cell(2) > 0) || (get_memory_cell(3) > 0))
               set_flag(get_memory_cell(2),get_memory_cell(3),1);
           open = 1;
           }
       else block_entry();
break;
beginstate DISPEL_BARRIER_STATE;
       if ((get_memory_cell(4) != 0 && open == 0)) {
           play_sound(9);
           print_str_color("Dispel Barrier: The magical part of the lock has been removed. (It can now be picked normally.)", 2);
           set_terrain_memory_cell(my_number(), 4, 0);
           }
break;